package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.*;
import com.market.bean.bo.admin.AdminCreateGoodsBeanBo;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminCatAndBrandVo;
import com.market.bean.vo.admin.AdminDetailGoodsBeanVo;
import com.market.bean.vo.admin.BrandListBeanVo;
import com.market.bean.vo.admin.CategoryListBeanVo;
import com.market.mapper.*;
import com.market.service.admin.AdminGoodsService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wh194@foxmail.com
 * @since 2022/12/29 22:31
 */
@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketBrandMapper brandMapper;

    @Autowired
    MarketGoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    MarketGoodsProductMapper goodsProductMapper;
    @Autowired
    MarketGoodsAttributeMapper goodsAttributeMapper;


    /**
     * 商品列表
     *
     * @param info
     * @param goodsSn
     * @param name
     * @param goodsId
     * @return
     */
    @Override
    public CommonData<MarketGoods> list(BasePageInfo info, String goodsSn, String name, Integer goodsId) {
        MarketGoodsExample example = new MarketGoodsExample();
        if (info != null && info.getLimit() != 0) {
            PageHelper.startPage(info.getPage(), info.getLimit());
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        } else {
            PageHelper.startPage(0, 0, true, true, true);
        }
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (goodsId != null) {
            criteria.andIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> list = goodsMapper.selectByExampleWithBLOBs(example);

        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }


    /**
     * 删除商品
     *
     * @param marketGoods
     */
    @Override
    public void delete(MarketGoods marketGoods) {
        marketGoods.setDeleted(true);
        goodsMapper.updateByPrimaryKeySelective(marketGoods);
    }


    @Override
    @Transactional
    public void create(AdminCreateGoodsBeanBo createGoodsBeanBo) {
        Date date = new Date();
        MarketGoods goods = createGoodsBeanBo.getGoods();
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        goodsMapper.insertSelective(goods);
        List<MarketGoodsSpecification> specifications = createGoodsBeanBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            specification.setGoodsId(goods.getId());
            goodsSpecificationMapper.insertSelective(specification);
        }
        List<MarketGoodsProduct> products = createGoodsBeanBo.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setAddTime(date);
            product.setUpdateTime(date);
            product.setGoodsId(goods.getId());
            goodsProductMapper.insertSelective(product);
        }
        List<MarketGoodsAttribute> attributes = createGoodsBeanBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            attribute.setGoodsId(goods.getId());
            goodsAttributeMapper.insertSelective(attribute);
        }
    }

    @Override
    public AdminCatAndBrandVo catAndBrand() {
        List<CategoryListBeanVo> list = categoryMapper.selectLabelByLevel("L1");
        for (CategoryListBeanVo beanVo : list) {
            List<CategoryListBeanVo> list2 = categoryMapper.selectLabelByPid(beanVo);
            beanVo.setChildren(list2);
        }
        List<BrandListBeanVo> list3 = brandMapper.selectAllBrand();
        AdminCatAndBrandVo data = new AdminCatAndBrandVo();
        data.setCategoryList(list);
        data.setBrandList(list3);

        return data;
    }

    @Override
    public AdminDetailGoodsBeanVo detail(Integer id) {
        AdminDetailGoodsBeanVo data = new AdminDetailGoodsBeanVo();
        MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(id);
        data.setGoods(marketGoods);

        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = marketGoodsAttributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketGoodsAttribute> marketGoodsAttributes = goodsAttributeMapper.selectByExample(marketGoodsAttributeExample);
        data.setAttributes(marketGoodsAttributes);

        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria1 = marketGoodsSpecificationExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        criteria1.andDeletedEqualTo(false);
        List<MarketGoodsSpecification> marketGoodsSpecifications = goodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);
        data.setSpecifications(marketGoodsSpecifications);

        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria2 = marketGoodsProductExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        criteria2.andDeletedEqualTo(false);
        List<MarketGoodsProduct> marketGoodsProducts = goodsProductMapper.selectByExample(marketGoodsProductExample);
        data.setProducts(marketGoodsProducts);

        List<Integer> integers = new ArrayList<>();
        integers.add(marketGoods.getCategoryId());
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(marketGoods.getCategoryId());
        if (marketCategory!=null&&marketCategory.getPid() != 0) {
            MarketCategory marketCategory1 = categoryMapper.selectByPrimaryKey(marketCategory.getPid());
            integers.add(marketCategory1.getId());
        }

        data.setCategoryIds(integers);
        return data;
    }

    @Override
    public void update(AdminCreateGoodsBeanBo createGoodsBeanBo) {
        Date date = new Date();
        MarketGoods goods = createGoodsBeanBo.getGoods();
        goods.setUpdateTime(date);
        goodsMapper.updateByPrimaryKeySelective(goods);
        List<MarketGoodsSpecification> specifications = createGoodsBeanBo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setUpdateTime(date);
            goodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }
        List<MarketGoodsProduct> products = createGoodsBeanBo.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setUpdateTime(date);
            goodsProductMapper.updateByPrimaryKeySelective(product);
        }
        List<MarketGoodsAttribute> attributes = createGoodsBeanBo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(date);
            goodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }
    }


}
